package com.cooper.controllers;

import java.sql.Timestamp;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.core.JsonResult;
import com.cooper.domain.UserSession;
import com.cooper.entity.UserInfo;
import com.cooper.security.PasswordGeneration;
import com.cooper.service.core.UserService;
import com.cooper.util.Controllers;

public class UserController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;
	
	@Post("userNotExists")
	public Object userNotExists(JsonResult json, @Param("userName") String userName) throws Exception{
		boolean result = this.userService.existsUserName(userName);
		json.setSuccess(!result);
		return json;
	}
	
	@Get("checkLogin")
	public Object checkLogin(Invocation inv, JsonResult json){
		Long userId = Controllers.getUserId(inv);
		if(null == userId || 0 == userId){
			
		} else {
			json.setSuccess(true);
		}
		return json;
	}
	
	@Post("savePassword")
	public Object savePassword(Invocation inv,@Param("oldPassword") String oldPassword,@Param("newPassword") String newPassword){
		JsonResult json = new JsonResult();
		String oldInput="";
		String newInput = "";
		try {
			oldInput = PasswordGeneration.encrypt(oldPassword);
			newInput = PasswordGeneration.encrypt(newPassword);
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMessage("you old password is not correct,please check");
		}
		String old = Controllers.getUserInfo(inv).getUserPassword();
		
		if(null == old || null == oldInput||!oldInput.equals(old)){
			json.setSuccess(false);
			json.setMessage("you old password is not correct,please check");
		} else if(newInput==null||newInput.length()<=0){
			json.setSuccess(false);
			json.setMessage("you new password is invalid");
		}else {
			UserInfo userInfo = userService.loadById(Controllers.getUserInfo(inv).getId());
			userInfo.setUserPassword(newInput);
			userService.changePassword(userInfo);
			json.setSuccess(true);
		}
		return json;
	}
	
	@Post("saveEmail")
	public Object saveEmail(Invocation inv,@Param("email") String email,@Param("password") String password){
		JsonResult json = new JsonResult();
		String passwd="";
		try {
			passwd = PasswordGeneration.encrypt(password);
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMessage("you password is not correct,please check");
		}
		String old = Controllers.getUserInfo(inv).getUserPassword();
		
		if(null == old || null == passwd||!passwd.equals(old)){
			json.setSuccess(false);
			json.setMessage("you password is not correct,please check");
		} else if(email==null||email.length()<=0){
			json.setSuccess(false);
			json.setMessage("you new email is invalid");
		}else {
			userService.updateEmail(Controllers.getUserInfo(inv).getId(), email);
			json.setSuccess(true);
		}
		return json;
	}
	
	@Post("saveBasic")
	public Object saveBasic(Invocation inv,UserInfo info){
		JsonResult json = new JsonResult();
		try {
			if(info!=null&&info.getId()!=null){
				UserInfo data = userService.loadById(info.getId());
				if(data!=null){
					data.setBirthday(info.getBirthday());
					data.setGender(info.getGender());
					data.setGmtModified(new Timestamp(System.currentTimeMillis()));
					data.setUserNick(info.getUserNick());
					data.setUserFileId(info.getUserFileId());
					userService.updateUser(data);
					
					json.setSuccess(true);
					UserSession userSession = Controllers.getUserSession(inv);
					userSession.setDesignerPhoto(info.getUserFileId());
					Controllers.setUserSession(inv, userSession);
				}
			}
		} catch (Exception e) {
			json.setSuccess(false);
			logger.error(e.getMessage(),e);
		}
		
		return json;
	}
}
